package com.cloud.utils;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TaskManager {
    Logger logger = LoggerFactory.getLogger(TaskManager.class);
    @Autowired
    private Scheduler scheduler;


    /**
     * 增加一个定时任务
     */
    public void addTask(String taskName,String group,String cron, Class c) {
        JobDetail jobDetail = JobBuilder.newJob(c).withIdentity(taskName, group)
                .build();
        CronTrigger cronTrigger = (CronTrigger) TriggerBuilder.newTrigger()
                .withIdentity(taskName, group)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
        try {
            scheduler.scheduleJob(jobDetail, cronTrigger);
            if (!scheduler.isStarted()) {
                scheduler.start();
            }
        } catch (SchedulerException e) {
            logger.error("【添加定时任务失败】 - " + e.getMessage());
        }
    }

    /**
     * 删除定时任务
     *
     * @param jobName
     * @param jobGroup
     * @throws Exception
     */
    public void removeTask(String jobName, String jobGroup) throws Exception {
        try {
            scheduler.deleteJob(new JobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            logger.error("【删除定时任务失败】- " + e.getMessage());
        }
    }

    /**
     * 编辑定时任务
     *
     * @param
     * @throws Exception
     */
    public void editTask(String jobName, String jobGroup, String cron) throws Exception {
        try {
            TriggerKey triggerKey = new TriggerKey(jobName, jobGroup);
            CronTrigger newTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                    .build();
            scheduler.rescheduleJob(triggerKey, newTrigger);
        } catch (Exception e) {
            logger.error("【更新定时任务失败】- " + e.getMessage());
        }
    }

    /**
     * 暂停任务
     */
    public void standByTask(String jobName, String jobGroup) throws Exception {
        try {
            JobKey jobKey = new JobKey(jobName, jobGroup);
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            logger.error("【暂停定时任务失败】 - " + e.getMessage());
        }
    }

    /**
     * 重启任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    public void resumeTask(String jobName, String jobGroup) throws Exception {
        try {
            scheduler.resumeJob(new JobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            logger.error("【重启定时任务失败】  - " + e.getMessage());
        }
    }

    /**
     * 马上执行一个定时任务
     *
     * @param id       执行失败的ID
     * @param jobName  执行任务
     * @param jobGroup 任务组
     */
    public void executeTaskNow(String id, String jobName, String jobGroup, Class c) {
        JobDetail jobDetail = JobBuilder.newJob(c).usingJobData("id", id).withIdentity(jobName, jobGroup).build();
        //10s后执行----
        Date date=new Date();
        date.setTime(date.getTime()+10*1000);
        Trigger trigger = TriggerBuilder.newTrigger().startAt(date).withIdentity(jobName, jobGroup).withSchedule(SimpleScheduleBuilder.repeatMinutelyForTotalCount(1)).build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
            if (!scheduler.isStarted()) {
                scheduler.start();
            }
        } catch (SchedulerException e) {
            logger.error("【重新添加失败的任务失败】", e.getMessage());
        }
    }
}
